#include "global_vars.h"

int setsock_nonblock(int fd) {
	int flags;

	flags = fcntl(fd, F_GETFL);
	if (flags < 0)
		return flags;

	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
		return -1;

	return 0;
}
int create_tcp_client_socket(const char *ip, int port) {
	struct sockaddr_in server_addr;
	int activate = 1;
	int e = -1;
	int sid;
	memset(&server_addr, '\0', sizeof(struct sockaddr_in));

	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));
	//setsock_nonblock(sid);

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	while (e < 0) {
		e = connect(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
		if (e >= 0)
			break;

		close(sid);
		sleep(1);
		sid = socket(AF_INET, SOCK_STREAM, 0);
		//setsock_nonblock(sid);
	}
	//setsock_nonblock(sid);
	return sid;
}
//------------------------------------------------------------------------------
int create_tcp_server_socket(const char *ip, int port) {

	struct sockaddr_in server_addr, client_addr;
	int sid, e, activate = 1;
	unsigned int client_addr_len;

	client_addr_len = sizeof(client_addr);
	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	e = bind(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
	assert(e >= 0);
	e = listen(sid, 1);

	printf("Server: PID: %d Waiting for connection requests from predecessor...\n", get_config_info()->self_id);
	int new_sid = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
	//setsock_nonblock(new_sid);
	printf("connection request came from the node  on socket: %d\n", new_sid);
	return new_sid;
}
void create_tcp_server_socket_for_star(char * ip, int port, int expected_num_connections, int sids[MAX_RING_NODES]) {
	unsigned int client_addr_len;
	int sid, e = -1, c_counter = 0, activate = 1;
	struct sockaddr_in server_addr, client_addr;

	client_addr_len = sizeof(client_addr);

	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	e = bind(sid, (struct sockaddr*) &server_addr, sizeof(server_addr));
	assert(e >= 0);

	e = listen(sid, expected_num_connections);
	assert(e >= 0);

	printf("\nLeader waiting for connection requests from processes on the ring...%d\n", expected_num_connections);
	while (c_counter < expected_num_connections) {

		sids[c_counter] = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
		setsockopt(sids[c_counter], SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

		while (sids[c_counter] < 0)
			sids[c_counter] = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
		c_counter++;
		printf("Number of requests received: %d\n", c_counter);
	}
}
